#include "CETargetViewer.h"

#include "SP.h"
#include "CECascadeSystem.h"

using namespace SP;

BEGIN_EVENT_TABLE( CETargetViewer, wxPanel )
	EVT_PAINT( CETargetViewer::OnPaint )
	EVT_MOUSE_EVENTS( CETargetViewer::OnMouseEvent )
	EVT_SIZE( CETargetViewer::OnSize )
	EVT_TIMER( idTimer, CETargetViewer::OnTimer )
END_EVENT_TABLE()

CETargetViewer::CETargetViewer( wxWindow* parent ) :
	wxPanel( parent ),
	m_Timer( this, idTimer )
{
	HWND hWnd = GetHandle();
	UINT width = GetClientRect().GetWidth();
	UINT height = GetClientRect().GetHeight();
	CEGetCascadeSystem()->Create( hWnd, width, height );

	m_Timer.Start( 6 );

}

CETargetViewer::~CETargetViewer( void )
{

}

void CETargetViewer::OnSize( wxSizeEvent& evt )
{
	wxSize newSize = evt.GetSize();
	UINT width = wxMax( 1, newSize.GetWidth() );
	UINT height = wxMax( 1, newSize.GetHeight() );
	
	CEGetCascadeSystem()->SetWindow( width, height );
}

void CETargetViewer::OnPaint( wxPaintEvent& evt )
{
	CEGetCascadeSystem()->OnFrame();
	evt.Skip();
}

void CETargetViewer::OnMouseEvent( wxMouseEvent& evt )
{
	wxPoint pt( evt.GetPosition() );
	SPModelViewerCamera* pCamera = CEGetCascadeSystem()->GetCamera();

	if ( evt.LeftDown() )
	{
		pCamera->OnBegin( pt.x, pt.y );
		SetFocus();
	}
	else if ( evt.Dragging()&&evt.LeftIsDown() )
	{
		pCamera->OnMove( pt.x, pt.y );
		//CEGetCascadeSystem()->OnFrame();
	}
	else if ( evt.LeftUp() )
	{
		pCamera->OnEnd();
	}

	if ( evt.GetWheelRotation() != 0 )
	{
		if ( evt.GetWheelRotation() > 0 )
			pCamera->Closer();
		else
			pCamera->Further();
		//CEGetCascadeSystem()->OnFrame();
	}
	/**/
}

void CETargetViewer::OnTimer( wxTimerEvent& event )
{
	CEGetCascadeSystem()->OnFrame();
}
